$(function () {
    $("#jqGrid").Grid({
        url: '../activity/list',
        colModel: [{
            label: 'id', name: 'id', index: 'id', key: true, hidden: true
        }, {
            label: '发起人用户Id', name: 'sendUserId', index: 'send_user_id', width: 60
        }, {
            label: '接收人用户Id', name: 'recvUserId', index: 'recv_user_id', width: 60
        }, {
            label: '状态', name: 'status', index: 'status', width: 60, formatter: function (value) {
                if (value == '1') {
                    return '未参加';
                } else if (value == '2') {
                    return '已参加';
                }
                return value;
            }
        }, {
            label: '发起时间', name: 'sendTime', index: 'send_time', width: 100, formatter: function (value) {
                return transDate(value);
            }
        }, {
            label: '参与时间', name: 'recvTime', index: 'recv_time', width: 100, formatter: function (value) {
                return transDate(value);
            }
        }]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        activity: { sendUserId: '', recvUserId: '', status: '', sendTime: '', recvTime: ''},
        ruleValidate: {
            status: [
                {required: true, message: '状态不能为空', trigger: 'blur'}
            ]
        },
        q: {
            userId: '',
            status: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.activity = {sendUserId: '', recvUserId: '', status: '', sendTime: '', recvTime: ''};
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.activity.id == null ? "../activity/save" : "../activity/update";
            console.log(JSON.stringify(vm.activity));
            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.activity),
                successCallback: function () {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {

                Ajax.request({
                    type: "POST",
                    url: "../activity/delete",
                    contentType: "application/json",
                    params: JSON.stringify(ids),
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },


        getInfo: function (id) {
            Ajax.request({
                url: "../activity/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.activity = r.activity;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'userId': vm.q.userId,
                    'status': vm.q.status},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});